#include "widget.h"
#include "ui_widget.h"
#include <synchapi.h>


Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    ui->lineEdit_pwd->setEchoMode(QLineEdit::Password);
    this->buf.append("0 0 0 0");
    this->tableview_setmodel();

    //创建定时器timer
    this->timer = new QTimer(this);

    //初始化数据库
    this->sql.createConnection();
    this->usersql.createConnection();


    //创建表
    this->sql.createTable();
    this->usersql.createTable();


    //默认登录界面
    ui->stackedWidget->setCurrentWidget(ui->page_login);

    //socket创建
    this->socket = new QTcpSocket;
    //建立链接
    socket->connectToHost(QHostAddress("192.168.1.233"), 8888);

    connect(socket, SIGNAL(readyRead()), this, SLOT(recvInfo()));
    connect(timer, SIGNAL(timeout()), this, SLOT(update()));


    ui->buzShow->setGeometry(260,230,55,50);
    ui->buzShow->setIconSize(QSize(65,55));
    ui->buzShow->setIcon(QIcon(":/img/red.jpg"));

    ui->fanShow->setGeometry(80,230,55,50);
    ui->fanShow->setIconSize(QSize(65,55));
    ui->fanShow->setIcon(QIcon(":/img/red.jpg"));

    ui->ligShow->setGeometry(440,230,55,50);
    ui->ligShow->setIconSize(QSize(65,55));
    ui->ligShow->setIcon(QIcon(":/img/red.jpg"));



}

Widget::~Widget()
{
    this->timer->stop();
    delete ui;
}

//登录按钮
void Widget::on_pushButton_clicked()
{
    QString name = ui->lineEdit_name->text();
    QString passwd = ui->lineEdit_pwd->text();
    bool flag = usersql.checkInfo(name, passwd);
    qDebug() << "flag = " << flag;
    if(true == flag)
    {
        ui->stackedWidget->setCurrentWidget(ui->page_choose);
    }
    else
    {
        QMessageBox::about(this,"提示","账号或者密码不正确,请重新输入!");
        ui->lineEdit_name->clear();
        ui->lineEdit_pwd->clear();
    }
    //启动定时器
    this->timer->start(1000);
}

//商品信息button
void Widget::on_pushButton_6_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->page_main);
}

void Widget::on_pushButton_8_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->page_choose);
}

void Widget::on_pushButton_13_clicked()
{
    ui->lineEdit_name->clear();
    ui->lineEdit_pwd->clear();
    ui->stackedWidget->setCurrentWidget(ui->page_login);
}

void Widget::on_pushButton_3_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->page_choose);
}

void Widget::on_pushButton_7_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->page_dev);
}

//注册按钮
void Widget::on_pushButton_2_clicked()
{
    //跳转至注册界面
    ui->stackedWidget->setCurrentWidget(ui->page_rigister);
}

//确认注册操作
void Widget::on_pushButton_4_clicked()
{
    //向数据库插入信息
    QString userName = ui->lineEdit_3->text();
    QString pwd1 = ui->lineEdit_4->text();
    QString pwd2 = ui->lineEdit_5->text();
    int no = ui->lineEdit_6->text().toInt();
    if(0 == no || userName == "")
    {
        QMessageBox::about(this, "提示","no或者用户名不能为0!");
        ui->lineEdit_3->clear();
        ui->lineEdit_4->clear();
        ui->lineEdit_5->clear();
        ui->lineEdit_6->clear();
        ui->stackedWidget->setCurrentWidget(ui->page_login);
        return;
    }
    if(true == this->usersql.selectUser(no))
    {
        QMessageBox::about(this, "提示","该用户已经注册!");
        ui->lineEdit_3->clear();
        ui->lineEdit_4->clear();
        ui->lineEdit_5->clear();
        ui->lineEdit_6->clear();
        ui->stackedWidget->setCurrentWidget(ui->page_login);
        return;
    }
    if(pwd1 != pwd2)
    {
        QMessageBox::about(this, "提示","两次输入的密码不正确!");
    }
    else
    {
        qDebug() << "no = " << no;
        qDebug() << "userName = " << userName;
        qDebug() << "pwd1 = "  << pwd1;

        if(true == this->usersql.insertData(no, userName, pwd1))
        {
            QMessageBox::about(this, "提示", "注册成功!");
        }
        else
        {
            QMessageBox::about(this, "提示", "注册失败!");
        }

    }
    ui->lineEdit_3->clear();
    ui->lineEdit_4->clear();
    ui->lineEdit_5->clear();
    ui->lineEdit_6->clear();
    ui->stackedWidget->setCurrentWidget(ui->page_login);
}

//取消注册操作
void Widget::on_pushButton_5_clicked()
{
    ui->lineEdit_3->clear();
    ui->lineEdit_4->clear();
    ui->lineEdit_5->clear();
    ui->lineEdit_6->clear();
    ui->stackedWidget->setCurrentWidget(ui->page_login);
}

void Widget::tableview_setmodel()
{
    //设置表头信息
    QStandardItemModel *model = new QStandardItemModel();
    model->setColumnCount(3);
    model->setHeaderData(0,Qt::Horizontal,"商品id");
    model->setHeaderData(1,Qt::Horizontal,"商品名称");
    model->setHeaderData(2,Qt::Horizontal,"库存数量");

    ui->tableView->setModel(model);
    ui->tableView->horizontalHeader()->setDefaultAlignment(Qt::AlignLeft);

    //禁止编辑
    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
}

//插入按钮
void Widget::on_pushButton_16_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->page_insert);
}

//确认插入操作
void Widget::on_pushButton_22_clicked()
{
    int id = ui->lineEdit->text().toInt();
    QString name = ui->lineEdit_2->text();
    int number = ui->lineEdit_11->text().toInt();

    //id有效性检查
    if(0 == id)
    {
        QMessageBox::about(this,"系统提示", "商品的id不能为0!");

        ui->stackedWidget->setCurrentWidget(ui->page_main);
        //清空lineEdit
        ui->lineEdit->clear();
        ui->lineEdit_2->clear();
        ui->lineEdit_11->clear();
        return;
    }
    int ret = this->sql.queryData(id);
    qDebug() << "ret = " << ret;
    if(0 != ret)
    {
        QMessageBox::about(this, "系统提示","该商品id已经存在!");
    }
    else
    {
        this->sql.insertData(id, name, number);
        QMessageBox::about(this,"系统提示", "插入成功!");
    }

    //返回主界面
    ui->stackedWidget->setCurrentWidget(ui->page_main);

    //清空lineEdit
    ui->lineEdit->clear();
    ui->lineEdit_2->clear();
    ui->lineEdit_11->clear();
}

//取消插入操作
void Widget::on_pushButton_23_clicked()
{
    //返回主界面
    ui->stackedWidget->setCurrentWidget(ui->page_main);

    //清空lineEdit
    ui->lineEdit->clear();
    ui->lineEdit_2->clear();
    ui->lineEdit_11->clear();
}

//查询按钮
void Widget::on_pushButton_18_clicked()
{
    //返回表中数据个数
    int ret = this->sql.queryAll(goods, 10);
    //绘制tableview的表头
    QStandardItemModel *model = new QStandardItemModel();
    model->setColumnCount(3);
    model->setHeaderData(0,Qt::Horizontal,"商品id");
    model->setHeaderData(1,Qt::Horizontal,"商品名称");
    model->setHeaderData(2,Qt::Horizontal,"库存数量");
    ui->tableView->setModel(model);
    ui->tableView->horizontalHeader()->setDefaultAlignment(Qt::AlignLeft);
    //禁止编辑
    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    //显示表中的数据
    for(int i = 0; i < ret; i++)
    {
        model->setItem(i,0,new QStandardItem(QString::number(goods[i].getId())));
        model->setItem(i,1,new QStandardItem(goods[i].getName()));
        model->setItem(i,2,new QStandardItem(QString::number(goods[i].getNumber())));
    }
}

//入库按钮
void Widget::on_pushButton_21_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->page_push);
}

//确认入库操作
void Widget::on_pushButton_24_clicked()
{
    int id = ui->lineEdit_12->text().toInt();
    if(0 == id || 0 == sql.queryData(id))
    {
        QMessageBox::about(this, "系统提示","输入商品的id不正确!");
        ui->lineEdit_12->clear();
        ui->lineEdit_13->clear();
        ui->stackedWidget->setCurrentWidget(ui->page_main);
        return;
    }

    int n = ui->lineEdit_13->text().toInt();
    int ret = sql.queryData(id);
    int new_n = ret + n;
    if(true == sql.updateOne(id, new_n))
    {
        QMessageBox::about(this,"提示","入库成功!");
    }
    else
    {
        QMessageBox::about(this,"提示","入库失败!");
    }

    ui->lineEdit_12->clear();
    ui->lineEdit_13->clear();
    ui->stackedWidget->setCurrentWidget(ui->page_main);
}

//取消入库操作
void Widget::on_pushButton_25_clicked()
{
    ui->lineEdit_13->clear();
    ui->lineEdit_12->clear();
    ui->stackedWidget->setCurrentWidget(ui->page_main);
}

//出库按钮
void Widget::on_pushButton_20_clicked()
{
     ui->stackedWidget->setCurrentWidget(ui->page_pop);
}

//确认出库操作
void Widget::on_pushButton_26_clicked()
{
    //判断商品id是否有效
    int id = ui->lineEdit_14->text().toInt();
    if(0 == id || 0 == sql.queryData(id))
    {
        QMessageBox::about(this, "系统提示","输入商品的id不正确!");
        ui->lineEdit_14->clear();
        ui->lineEdit_15->clear();
        ui->stackedWidget->setCurrentWidget(ui->page_main);
        return;
    }

    int n = ui->lineEdit_15->text().toInt();
    int ret = sql.queryData(id);
    int new_n = ret - n;

    if(new_n <= 0)
    {
        new_n = 0;
        //在表中删除这个商品
        sql.deleteData(id);
        QMessageBox::about(this, "提示", "出库成功!");
    }
    else
    {
        if(true == sql.updateOne(id, new_n))
        {
            QMessageBox::about(this, "提示", "出库成功!");
        }
        else
        {
            QMessageBox::about(this, "提示", "出库失败!");
        }
    }
    ui->lineEdit_14->clear();
    ui->lineEdit_15->clear();
    ui->stackedWidget->setCurrentWidget(ui->page_main);
}

//取消出库操作
void Widget::on_pushButton_27_clicked()
{
    ui->lineEdit_14->clear();
    ui->lineEdit_15->clear();
    ui->stackedWidget->setCurrentWidget(ui->page_main);
}

//删除按钮
void Widget::on_pushButton_19_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->page_del);
}

//确认删除操作
void Widget::on_pushButton_28_clicked()
{
    //获取输入id
    int id = ui->lineEdit_16->text().toInt();

    //判断id是否为其他错误输入字符
    if(0 == id)
    {
        QMessageBox::about(this, "系统提示","商品的id不能为0!");
        ui->lineEdit_16->clear();
        ui->stackedWidget->setCurrentWidget(ui->page_main);
        return;
    }

    //判断id是否存在
    int ret = this->sql.queryData(id);
    if(0 == ret)
    {
        QMessageBox::about(this, "系统提示","商品不存在!");
    }
    else
    {
        if(true == sql.deleteData(id))
        {
            QMessageBox::about(this, "系统提示","商品删除成功!");
        }
        else
        {
            QMessageBox::about(this, "系统提示","商品删除失败!");
        }
    }
    ui->lineEdit_16->clear();
    ui->stackedWidget->setCurrentWidget(ui->page_main);
}

//取消删除操作
void Widget::on_pushButton_29_clicked()
{
    ui->lineEdit_16->clear();
    ui->stackedWidget->setCurrentWidget(ui->page_main);
}

//修改button
void Widget::on_pushButton_17_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->page_update);
}

//确认修改操作
void Widget::on_pushButton_9_clicked()
{
    //获取输入的商品信息
    int id = ui->lineEdit_7->text().toInt();
    int new_id = ui->lineEdit_8->text().toInt();
    QString new_name = ui->lineEdit_9->text();
    int new_num = ui->lineEdit_10->text().toInt();

    //判断商品id的有效性
    if(0 == id || 0 == sql.queryData(id))
    {
        QMessageBox::about(this, "系统提示", "输入的商品id不存在!");
    }
    else
    {
        if(0 == new_id)
        {
            QMessageBox::about(this, "系统提示", "输入新商品的id不合法!");
        }
        else
        {
            //删除旧的商品信息
            this->sql.deleteData(id);
            //插入新的商品信息
            this->sql.insertData(new_id, new_name, new_num);
            QMessageBox::about(this, "系统提示", "更新成功!");
        }
    }

    //返回主界面
    ui->stackedWidget->setCurrentWidget(ui->page_main);

    //清空编辑框
    ui->lineEdit_7->clear();
    ui->lineEdit_8->clear();
    ui->lineEdit_9->clear();
    ui->lineEdit_10->clear();
}

//取消修改操作
void Widget::on_pushButton_30_clicked()
{
    //清空编辑框
    ui->lineEdit_7->clear();
    ui->lineEdit_8->clear();
    ui->lineEdit_9->clear();
    ui->lineEdit_10->clear();

    //返回主界面
    ui->stackedWidget->setCurrentWidget(ui->page_main);
}

//接收数据
void Widget::recvInfo()
{
    //不断更新buff
    buf = socket->readAll();
}


//定时器槽函数
void Widget::update()
{
    //更新显示的内容
    QStringList list = buf.split(' ');

    ui->tempShow->setText(list[0]);
    ui->humShow->setText(list[1]);
    ui->lightShow->setText(list[2]);
    ui->serverShow->setText(list[3]);

}
//温度控制
void Widget::on_tempControl_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->page_temp);
}
//向服务器发送温度数据
void Widget::on_sendTemp_clicked()
{
    QString str1;
    str1 = ui->tempEdit->text();
    qDebug() << str1 << endl;

    QByteArray buf1 = str1.toUtf8();
    socket->write("7"+buf1);
    ui->tempEdit->clear();
    ui->stackedWidget->setCurrentWidget(ui->page_dev);
}
//取消设置温度
void Widget::on_pushButton_32_clicked()
{
    ui->tempEdit->clear();
    ui->stackedWidget->setCurrentWidget(ui->page_dev);
}
//向服务器发送湿度数据
void Widget::on_sendHum_clicked()
{
    QString str2;
    str2 = ui->humEdit->text();
    qDebug() << str2 << endl;

    QByteArray buf2 = str2.toUtf8();
    socket->write("9"+buf2);
    ui->humEdit->clear();
    ui->stackedWidget->setCurrentWidget(ui->page_dev);
}
//取消设置湿度
void Widget::on_pushButton_11_clicked()
{
    ui->humEdit->clear();
    ui->stackedWidget->setCurrentWidget(ui->page_dev);
}
//湿度控制
void Widget::on_humControl_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->page_hum);
}
//光照控制
void Widget::on_lightControl_clicked()
{
   ui->stackedWidget->setCurrentWidget(ui->page_light);
}

//向服务器发送光照值
void Widget::on_sendLight_clicked()
{
    QString str3;
    str3 = ui->lightEdit->text();
    qDebug() << str3 << endl;

    QByteArray buf3 = str3.toUtf8();
    socket->write("8"+buf3);
    ui->lightEdit->clear();
    ui->stackedWidget->setCurrentWidget(ui->page_dev);
}
//取消设置灯光
void Widget::on_pushButton_12_clicked()
{
    ui->lightEdit->clear();
    ui->stackedWidget->setCurrentWidget(ui->page_dev);
}

//风扇开
void Widget::on_fanOn_clicked()
{
    socket->write("3");
    ui->fanShow->setGeometry(80,230,55,50);
    ui->fanShow->setIconSize(QSize(65,55));
    ui->fanShow->setIcon(QIcon(":/img/greed.jpg"));

}
//风扇关
void Widget::on_fanOff_clicked()
{
    socket->write("4");
    ui->fanShow->setGeometry(80,230,55,50);
    ui->fanShow->setIconSize(QSize(65,55));
    ui->fanShow->setIcon(QIcon(":/img/red.jpg"));

}
//蜂鸣器开
void Widget::on_buzOn_clicked()
{
    socket->write("5");
    ui->buzShow->setGeometry(260,230,55,50);
    ui->buzShow->setIconSize(QSize(65,55));
    ui->buzShow->setIcon(QIcon(":/img/greed.jpg"));
}
//蜂鸣器关
void Widget::on_buzOff_clicked()
{
    socket->write("6");
    socket->write("5");
    ui->buzShow->setGeometry(260,230,55,50);
    ui->buzShow->setIconSize(QSize(65,55));
    ui->buzShow->setIcon(QIcon(":/img/red.jpg"));
}
//灯开
void Widget::on_lightOn_clicked()
{
    socket->write("1");
    ui->ligShow->setGeometry(440,230,55,50);
    ui->ligShow->setIconSize(QSize(65,55));
    ui->ligShow->setIcon(QIcon(":/img/greed.jpg"));
}
//灯关
void Widget::on_lightOff_clicked()
{
    socket->write("2");
    ui->ligShow->setGeometry(440,230,55,50);
    ui->ligShow->setIconSize(QSize(65,55));
    ui->ligShow->setIcon(QIcon(":/img/red.jpg"));
}
